package com.pineapple.module.core.hithormone.service;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.pineapple.module.core.hithormone.domain.Hithormone;
import com.pineapple.module.core.hithormone.domain.HithormoneCriteria;

public class HithormoneManager {
	HithormoneDAO dao = null;

	public HithormoneManager(){
		dao = new HithormoneDAO();
	}

	public List<Hithormone> search(HithormoneCriteria criteria,Connection conn)throws Exception{

		List<Hithormone> lstHithormone = new ArrayList<Hithormone>();
		int totalRacord =dao.count(criteria, conn);
		criteria.setTotalResult(totalRacord);

		if (criteria.getTotalResult() < criteria.getStart()) {
			criteria.setStart(criteria.getStart() - criteria.getLinePerPage());
		}
		if(totalRacord>0){
			lstHithormone = dao.search(criteria, conn);
		}
		return lstHithormone;
	}

	public Map<String,String> hormoneCombo(Connection conn)throws Exception{
		return dao.hormoneCombo(conn);
	}
	public Map<String,String> areaCombo(Connection conn)throws Exception{
		return dao.searchComboPlantAndArea(conn);
	}
	public void add(Connection conn,Hithormone hithormone)throws Exception{
		dao.add(conn, hithormone);
		if(Integer.parseInt(hithormone.getPlantingStatus())!=3 && Integer.parseInt(hithormone.getPlantingStatus())!=6 ){
			dao.updateStatus(conn, hithormone.getHithormoneAreaId() ,Integer.parseInt(hithormone.getPlantingStatus())+1);
		}
	}
	public void edit(Connection conn,Hithormone hithormone)throws Exception{
		dao.edit(conn, hithormone);
	}
	public void delete(Connection conn,String hithormoneIds,String plantingId,int status)throws Exception{
		dao.delete(conn, hithormoneIds);
		if(dao.countStatus(conn, plantingId)>0){
			dao.updateStatus(conn, plantingId , status-1);
		}
	}
	public Hithormone searchById(Connection conn,int id)throws Exception{
		return dao.searchById(conn, id);
	}

	public int countNumb(Connection conn,String areaId)throws Exception{
		return dao.countNumb(conn, areaId);
	}

}
